package com.cxl.demo.controller;

import com.cxl.demo.service.CommentService;
import com.cxl.demo.vo.Comment;
import com.cxl.demo.vo.ReturnData;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {

    @Autowired
    ReturnData returnData;
    @Autowired
    CommentService commentService;
    @Autowired
    User user;


    @PostMapping("AddComment")
    public ReturnData AddComment(@RequestBody Comment comment) {
        returnData.setData(null);

        Date date = new Date();
        String timestamp = String.valueOf(date.getTime());


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = timestamp;
        long l = Long.parseLong(currentTime);
        String format = sdf.format(l);


        comment.setUserId(user.getUserId());
        comment.setCreateTime(format);
        System.out.println(comment);
        Integer ok = commentService.addComment(comment);
        if (ok == 1) {
            returnData.setStatus(true);
            returnData.setMsg("评论成功");
        } else {
            returnData.setStatus(false);
            returnData.setMsg("评论失败");
        }

        return returnData;
    }

    @PostMapping("DelComment")
    public ReturnData DelComment(@RequestBody Map<String, Object> map) {
        returnData.setData(null);
        Integer ok = commentService.delComment((Integer) map.get("commentId"));

        if (ok == 1) {
            returnData.setStatus(true);
            returnData.setMsg("删除评论成功");
        } else {
            returnData.setStatus(false);
            returnData.setMsg("删除评论失败");
        }

        return returnData;
    }

    @PostMapping("GetCommentListByAssigment")
    public ReturnData GetCommentListByAssigment(@RequestBody Map<String, Object> map) {
        returnData.setData(null);

        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        returnData.setData(commentService.getCommentsBuAssigmentId((Integer) map.get("assigmentId")));
        return returnData;
    }

}
